package com.yf.modules.system.controller;


import com.yf.Utils.PageUtil;
import com.yf.domain.security.User;
import com.yf.model.query.UserQueryCriteria;
import com.yf.modules.system.model.vo.UserPassVO;
import com.yf.modules.system.service.api.UserService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2023/5/22 15:00
 * @description userRestful
 */
@RestController
@RequestMapping("/api/users")
@Slf4j
@Tag(name = "UserApi", description = "用户Api")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("")
    @Operation(summary = "获取用户信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> queryUserInfo(UserQueryCriteria criteria) {
        List<User> users = userService.queryUserinfo(criteria);
        return ResponseEntity.ok().body(ResponseBody.success(PageUtil.toPage(users, users.size())));
    }

    @PostMapping("/add")
    @Operation(summary = "添加新用户", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> addUser(@RequestBody User user) {
        userService.create(user);
        //TODO:测试
        return ResponseEntity.ok().body(ResponseBody.success("添加成功"));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改用户信息", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> editUser(@RequestBody User user) {
        userService.update(user);
        //TODO:测试
        return ResponseEntity.ok().body(ResponseBody.success("修改成功"));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<String>> deleteUser(@RequestBody Set<Long> ids) {
        userService.delete(ids);
        return ResponseEntity.ok().body(ResponseBody.success("删除成功"));
    }

    @GetMapping("/download")
    @Operation(summary = "导出用户报表", security = @SecurityRequirement(name = "authScheme"))
    public void downloadUserInfo(UserQueryCriteria criteria, HttpServletResponse response) {
        try {
            userService.download(criteria, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping(value = "/resetPwd")
    @Operation(summary = "重置密码", security = @SecurityRequirement(name = "authScheme"))
    public ResponseEntity<ResponseBody<Object>> resetPwd(@RequestBody Set<Long> ids) {
        userService.resetUserPassword(ids);
        return ResponseEntity.ok().body(ResponseBody.success("重置成功，默认密码：123456"));
    }


}
